//
//  SCOrderFlowChart.m
//  Shuaiche
//
//  Created by 党帅 on 2019/7/30.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCOrderFlowChart.h"

@implementation SCOrderFlowChart {
    NSArray *_titles;
}

- (instancetype)initWithFlowTitles:(NSArray *)array {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _titles = array;
        [self initialize];
    }
    return self;
}

- (void)setCurrentStep:(NSInteger)currentStep {
    if (_currentStep == currentStep) {
        return;
    }
    _currentStep = currentStep;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    if (!_titles) {
        return;
    }
    
    CGFloat line_h = 4.f;
    CGFloat w = CGRectGetWidth(self.frame) - 2 * _leftPadding - _thumbWidth;
    CGFloat item_w = floorf(w / (_titles.count - 1));
    CGFloat line_center_y = CGRectGetMidY(self.bounds) - line_h / 2;
    CGFloat offset_x = _thumbWidth /2;
    CGFloat start_x = _leftPadding + offset_x;
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context,line_h);
    CGContextSetRGBStrokeColor(context, 249/255.0, 152/255.0, 2/255.0, 1);
    
    CGContextMoveToPoint(context, start_x, line_center_y);
    CGContextAddLineToPoint(context, _currentStep * item_w + start_x, line_center_y);
    CGContextStrokePath(context);

    CGContextSetRGBStrokeColor(context, 1, 213/255.0, 149/255.0, 1);
    CGContextMoveToPoint(context, _currentStep * item_w + _leftPadding + offset_x, line_center_y);
    CGContextAddLineToPoint(context, start_x + w, line_center_y);
    CGContextStrokePath(context);
    
    NSDictionary *attrNor = @{NSFontAttributeName:_titleFont,NSForegroundColorAttributeName:_titleColor};
    NSDictionary *attrHig = @{NSFontAttributeName:_titleFont,NSForegroundColorAttributeName:_titleColorHighlight};
    
    CGContextSetLineWidth(context, 3);
    CGFloat r = (_thumbWidth - 3)/2.f;
    for (int i = 0; i < _titles.count; i++) {
        NSString *string = _titles[i];
        CGFloat w = [string boundingRectWithSize:CGSizeMake(100, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrNor context:nil].size.width;
        CGFloat y = (i & 1) ? (line_center_y - _lineSpacing - 11) :(line_center_y + _lineSpacing);
        NSDictionary *attri = i <= _currentStep ? attrHig : attrNor;
        
//        if (i == 0) {
//            [string drawAtPoint:CGPointMake(_leftPadding, y) withAttributes:attri];
//            CGContextAddArc(context, start_x, line_center_y, r, 0, M_PI * 2, YES);
//        } else {
            [string drawAtPoint:CGPointMake(item_w * i - w/2 + start_x, y) withAttributes:attri];
            CGContextAddArc(context, item_w * i + start_x, line_center_y, r, 0, M_PI * 2, YES);
//        }
        
        if (i <= _currentStep) {
            CGContextSetRGBStrokeColor(context, 249/255.0, 152/255.0, 2/255.0, 1);
            CGContextSetRGBFillColor(context, 249/255.0, 152/255.0, 2/255.0, 1);
        } else {
            CGContextSetRGBStrokeColor(context, 1, 213/255.0, 149/255.0, 1);
            CGContextSetRGBFillColor(context, 1, 1, 1, 1);
        }
        CGContextDrawPath(context, kCGPathFillStroke);
    }
}

- (void)initialize {
    _currentStep = 0;
    _lineSpacing = 16;
    _thumbWidth = 14;
    _leftPadding = 50;
    _titleFont = SC_FONT(10);
    _titleColorHighlight = SC_TextBlack_Primary;
    _titleColor = SC_TextBlack_Third;
}

@end
